<template>
	<view class="sun-page">
		
		<!-- 表单内容 -->
		<view class="form-container">			
			<!-- 标签编码 -->
			<view class="form-item">
				<view class="form-label required">标签编码</view>
				<view class="form-input">
					<u-input v-model="formData.label_code" placeholder="标签的唯一编码" border="none" :disabled="true"></u-input>
				</view>
			</view>
			
			<!-- 标签规格 -->
			<view class="form-item">
				<view class="form-label">标签规格</view>
				<view class="form-input">
					<u-input v-model="formData.label_specification" placeholder="请输入标签规格" border="none"></u-input>
				</view>
			</view>
			
			<!-- 天线规格 -->
			<view class="form-item">
				<view class="form-label">天线规格</view>
				<view class="form-input">
					<u-input v-model="formData.antenna_specification" placeholder="请输入天线规格" border="none"></u-input>
				</view>
			</view>
			
			<!-- 芯片规格 -->
			<view class="form-item">
				<view class="form-label">芯片规格</view>
				<view class="form-input">
					<u-input v-model="formData.chip_specification" placeholder="请输入芯片规格" border="none"></u-input>
				</view>
			</view>
			
			<!-- 标签材质 -->
			<view class="form-item">
				<view class="form-label">标签材质</view>
				<view class="form-input">
					<u-input v-model="formData.label_material" placeholder="请输入标签材质" border="none"></u-input>
				</view>
			</view>
			
			<!-- 标签类型 -->
			<view class="form-item">
				<view class="form-label">标签类型</view>
				<view class="form-input">
					<u-input v-model="formData.label_type" placeholder="请输入标签类型" border="none"></u-input>
				</view>
			</view>
		</view>
		
		<!-- 提交按钮 -->
		<view class="submit-btn">
			<u-button type="primary" text="提 交" @click="submitForm"></u-button>
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue';
	import http from "@/api/request";
	import { onLoad,onUnload} from "@dcloudio/uni-app";
	
	// 表单数据
	const formData = ref({
		id: '',
		label_code: '',
		label_specification: '',
		antenna_specification: '',
		chip_specification: '',
		label_material: '',
		label_type: 'RFID'
	});
	
	// 页面加载时获取参数
	onLoad((options) => {
		// 检查是否有传递的参数
		// if (options.params) {
			try {
				const labelData = uni.getStorageSync("assetLabel");
				// 填充表单数据
				formData.value = {
					id: labelData.id || '',
					label_code: labelData.label_code || '',
					label_specification: labelData.label_specification || '',
					antenna_specification: labelData.antenna_specification || '',
					chip_specification: labelData.chip_specification || '',
					label_material: labelData.label_material || '',
					label_type: labelData.label_type || 'RFID'
				};
			} catch (e) {
				console.error('解析参数错误:', e);
				uni.showToast({
					title: '获取标签数据失败',
					icon: 'none'
				});
			}
		// }
	});

	onUnload(() => {
		uni.removeStorageSync("assetLabel");
	});
	
	// 提交表单
	function submitForm() {
		// 表单验证
		if (!formData.value.label_code) {
			uni.showToast({
				title: '请输入标签编码',
				icon: 'none'
			});
			return;
		}
		
		// 显示加载中
		uni.showLoading({
			title: '提交中...'
		});
		
		// 构建请求参数
		const data = { 
			"pageCode": "rfidlabel", 
			"params": { 
				"args": [ 
					{ 
						"name": "interfaceCode", 
						"value": "rfidlabel.saveRfidlabel" 
					}, 
					{ 
						"name": "tagMstr", 
						"value": formData.value
					} 
				] 
			}, 
			"methodName": "saveRfidlabel" 
		};
		// 调用接口
		http.post("/api/module/Invoke", data).then((res) => {
			uni.hideLoading();
			
			if (res.isSuccess) {
				uni.showToast({
					title: '修改成功',
					icon: 'success'
				});
				
				// 触发全局事件，通知列表页刷新数据
				uni.$emit('labelDataChanged');
				
				// 提交成功后返回上一页
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			} else {
				uni.showToast({
					title: res.error || '修改失败',
					icon: 'none'
				});
			}
		}).catch(err => {
			uni.hideLoading();
			console.error('修改标签失败:', err);
			uni.showToast({
				title: '网络异常，请稍后重试',
				icon: 'none'
			});
		});
	}
</script>

<style>
	.sun-page {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f5f5f5;
	}
	
	.form-container {
		background-color: #FFFFFF;
		margin-top: 20rpx;
	}
	
	.form-item {
		display: flex;
		padding: 30rpx;
		border-bottom: 1px solid #f0f0f0;
	}
	
	.form-label {
		width: 200rpx;
		font-size: 30rpx;
		color: #333;
	}
	
	.required::before {
		content: '*';
		color: #f56c6c;
		margin-right: 4rpx;
	}
	
	.form-input {
		flex: 1;
	}
	
	.submit-btn {
		padding: 40rpx 30rpx;
		margin-top: auto;
	}
</style>